package com.qf.efserver.sport.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qf.common.core.entity.ActivityRecords;
import com.qf.common.core.entity.SportDay;
import com.qf.common.core.utils.UserUtils;
import com.qf.efserver.sport.mapper.SportDayMapper;
import com.qf.efserver.sport.mapper.SportMapper;
import com.qf.efserver.sport.param.SportParam;
import com.qf.efserver.sport.service.SportService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SportServiceImpl implements SportService {
    public final SportMapper sportMapper;
    public final SportDayMapper sportDayMapper;

    @Override
    public void addActivityRecords(SportParam sportParam) {
        ActivityRecords activityRecords = new ActivityRecords();
        BeanUtils.copyProperties(sportParam,activityRecords);
        activityRecords.setUserId(UserUtils.getUid());
        sportMapper.insert(activityRecords);
        //判断今天有没有运动记录
        // 没有就新增，有就修改
        SportDay sportDay = sportDayMapper.selectByUidCreateTime(UserUtils.getUid());
        if (sportDay==null){
            SportDay sportDay1 = new SportDay();
            sportDay1.setUserId(UserUtils.getUid());
            sportDay1.setDuration(activityRecords.getDuration());
            sportDay1.setDistance(activityRecords.getDistance());
            sportDay1.setSteps(activityRecords.getSteps());
            sportDay1.setCaloriesBurned(activityRecords.getCaloriesBurned());

            sportDay1.setTodayTime(LocalDate.now());
            sportDayMapper.insert(sportDay1);
        }else {
            sportDay.setDuration(sportDay.getDuration()+activityRecords.getDuration());
            sportDay.setDistance(sportDay.getDistance()+activityRecords.getDistance());
            sportDay.setSteps(sportDay.getSteps()+activityRecords.getSteps());
            sportDay.setCaloriesBurned(sportDay.getCaloriesBurned()+activityRecords.getCaloriesBurned());
            sportDayMapper.updateById(sportDay);
        }

    }

    @Override
    public List<ActivityRecords> selectActivityRecords(Integer timeId) {
        List<ActivityRecords> activityRecords = null;
        if (timeId==0){
            //全部
            activityRecords = sportMapper.selectList(null);
        }else if (timeId==1){
            //年份
            activityRecords = sportMapper.selectYear(UserUtils.getUid());
        }else if (timeId==3){
            //周
            activityRecords = sportMapper.selectWeek(UserUtils.getUid());
        }else if (timeId==2){
            //月
            activityRecords = sportMapper.selectMonth(UserUtils.getUid());
        }else if (timeId==4){
            //日
            activityRecords = sportMapper.selectDay(UserUtils.getUid());

        }

        return activityRecords;
    }

    @Override
    public Integer selectWeekCount(Integer weekId) {
        Integer weekCount = sportMapper.selectWeekCount(weekId, UserUtils.getUid());
        return weekCount;
    }

    /**
     * 添加活动
     *
     * @param sportParam
     * @return
     */
    @Override
    public Integer addActivity(SportParam sportParam) {
        ActivityRecords activityRecords = new ActivityRecords();
        activityRecords.setUserId(sportParam.getUserId());
        activityRecords.setActivityType(sportParam.getActivityType());
        sportMapper.insert(activityRecords);
        return activityRecords.getActivityId();
    }

    @Override
    public void updateActivityById(ActivityRecords activityRecords) {
        sportMapper.update(activityRecords, new LambdaUpdateWrapper<ActivityRecords>()
                .eq(ActivityRecords::getActivityId, activityRecords.getActivityId()));
    }


}
